//
//  Action_ViewController.m
//  EggSteward
//
//  Created by 王道林 on 16/9/2.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "Action_ViewController.h"
#import "DLMacro.h"
#import "DLWNetworkingParser.h"
#import "ShopGoodModel.h"
#import "ActionCycleModel.h"
#import "BaseCollectionView.h"
#import "Action_collectionViewCellCycle.h"
#import "Action_collectionViewCellGoods.h"
#import "Home_NewsViewControllerWeb.h"
#import "Scene_ViewController.h"
#import "Task_ViewController.h"
#import "LogonViewController.h"
@interface Action_ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *arrDataGood;
@property (nonatomic, strong) NSMutableArray *arrDataCycle;
@property (nonatomic, strong) BaseCollectionView *collectionView;
@property (nonatomic, strong) UIAlertController *alertController;
//记录下是否签到了
@property (nonatomic, copy) NSString *singIn;
@end

@implementation Action_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"活动中心";
    [self handleData];
    [self createSubView];
    
}


#pragma mark - 数据请求
- (void)handleData
{
    _arrDataGood = [NSMutableArray array];
    _arrDataCycle = [NSMutableArray array];
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *ticket = [userDefault valueForKey:@"ticket"];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:ticket,@"ticket", nil];
    [DLWNetworkingParser POSTWithURL:@"http://api.danjiguanjia.com/active/Index/goodlist" par:dic success:^(id responseObject) {
        // 商品数据
        for (NSDictionary *dic in responseObject[@"data"][@"data"]) {
            ShopGoodModel *model = [ShopGoodModel modelWithDic:dic];
            [_arrDataGood addObject:model];
        }
        // 轮播图数据
        for (NSDictionary *dic in responseObject[@"data"][@"banner"]) {
            ActionCycleModel *model = [ActionCycleModel modelWithDic:dic];
            [_arrDataCycle addObject:model];
        }
        [_collectionView reloadData];
    } filed:^(NSError *error) {
        
    }];
}


// 创建子控件
- (void)createSubView
{
        //
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _collectionView = [[BaseCollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    // 注册
    [_collectionView registerClass:[Action_collectionViewCellCycle class] forCellWithReuseIdentifier:@"reuseOne"];
    [_collectionView registerClass:[Action_collectionViewCellGoods class] forCellWithReuseIdentifier:@"reuseTwo"];
    
    // 创建alertController
    _alertController = [UIAlertController alertControllerWithTitle:@"消息提醒" message:@"签到成功" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [_alertController dismissViewControllerAnimated:YES completion:nil];
    }];
    [_alertController addAction:alertAction];
}

#pragma mark - collectionView的协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return _arrDataGood.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        Action_collectionViewCellCycle *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseOne" forIndexPath:indexPath];
        cell.blockCycleUrl = ^(NSString *url)
        {
            Home_NewsViewControllerWeb *webVC = [[Home_NewsViewControllerWeb alloc] init];
            webVC.name = @"actionCycle";
            webVC.url = url;
            webVC.navigationTitle = @"活动详情";
            [self.navigationController pushViewController:webVC animated:YES];
        };
        cell.arrData = _arrDataCycle;
        // block 带回的点击方法
        [cell buttonActionBlockSingIn:^{
            // 签到任务
            NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
            NSString *ticket = [userDefault valueForKey:@"ticket"];
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:ticket,@"ticket", nil];
            [DLWNetworkingParser POSTWithURL:@"http://api.danjiguanjia.com/v1/Score/sign" par:dic success:^(id responseObject) {
                NSString *score = responseObject[@"data"][@"score"];
                NSString *msg = responseObject[@"msg"];
                if ([msg isEqualToString:@"签到成功"]) {
                    NSString *message = [NSString stringWithFormat:@"签到成功:当前积分为%@", score];
                    _alertController.message = message;
                    [self presentViewController:_alertController animated:YES completion:nil];
                    
                } else
                {
                    _alertController.message = @"已经签到了";
                    [self presentViewController:_alertController animated:YES completion:nil];
                }
            } filed:^(NSError *error) {
                
            }];
        } blockTurn:^{
            // 推出转盘
            Home_NewsViewControllerWeb *webVC = [[Home_NewsViewControllerWeb alloc] init];
            webVC.name = @"TurnTable";
            webVC.navigationTitle = @"大转盘";
            [self.navigationController pushViewController:webVC animated:YES];
        } blockAction:^{
            // 推出现场活动界面
            Scene_ViewController *sceneVC = [[Scene_ViewController alloc] init];
            [self.navigationController pushViewController:sceneVC animated:YES];
        } blockTack:^{
            //  推出每日任务界面
            Task_ViewController *taskVC = [[Task_ViewController alloc] init];
            [self.navigationController pushViewController:taskVC animated:YES];
        }];
        return cell;
    }
    else
    {
    Action_collectionViewCellGoods *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseTwo" forIndexPath:indexPath];
    ShopGoodModel *model = _arrDataGood[indexPath.row];
    cell.shopGoddModel = model;
    return cell;
    }
}


#pragma mark - layout协议
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(VIEW_WIDTH, 300);
    } else
    {
        return CGSizeMake((VIEW_WIDTH - 32) / 2, 200);
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 0) {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    } else
    {
        return UIEdgeInsetsMake(0, 10, 0, 10);
    }
}

// 下面的商品点击.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        ShopGoodModel *model = _arrDataGood[indexPath.row];
        Home_NewsViewControllerWeb *webVC = [[Home_NewsViewControllerWeb alloc] init];
        webVC.name = @"shopGoodsDetail";
        webVC.newsId = model.goodID;
        webVC.navigationTitle = @"商品详情";
        [self.navigationController pushViewController:webVC animated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
